package com.kadron.dao;

import java.util.List;

import javax.persistence.EntityManager;

import org.jboss.seam.annotations.AutoCreate;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.intercept.BypassInterceptors;

import com.kadron.entity.Material;

@AutoCreate
@BypassInterceptors
@Name("materialDAO")
public class KMaterialDAO extends KDAO<Material> {

	@SuppressWarnings("unchecked")
	public List<Material> getMaterials(long itemtypeId) {
		EntityManager em = getEntityManager();
		return em.createQuery("SELECT DISTINCT p.material FROM ItemTypeMaterial p WHERE p.itemType.id = :it_id ORDER BY p.ordinal")
				.setParameter("it_id", itemtypeId)
				.getResultList();
	}
	
	@SuppressWarnings("unchecked")
	public List<Material> getAvailableMaterials(long ittemtypeId) {
		EntityManager em = getEntityManager();
		return em.createQuery("SELECT DISTINCT e FROM Material e WHERE NOT EXISTS(SELECT 1 FROM ItemTypeMaterial v WHERE v.material.id=e.id AND v.itemType.id=:it_id) ")
			.setParameter("it_id", ittemtypeId)
			.getResultList();
	}
	
}
